{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于模型协同过滤的推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'sys' (built-in)>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "import importlib\n",
    "importlib.reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOCKSGZ12A58A7CA4B</td>\n",
       "      <td>1</td>\n",
       "      <td>Bleed It Out [Live At Milton Keynes]</td>\n",
       "      <td>Road To Revolution: Live At Milton Keynes</td>\n",
       "      <td>Linkin Park</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOCVTLJ12A6310F0FD</td>\n",
       "      <td>1</td>\n",
       "      <td>Clocks</td>\n",
       "      <td>A Rush Of Blood To The Head</td>\n",
       "      <td>Coldplay</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SODLLYS12A8C13A96B</td>\n",
       "      <td>3</td>\n",
       "      <td>Breakeven</td>\n",
       "      <td>Now That's What I Call Music! 72</td>\n",
       "      <td>The Script</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOEGIYH12A6D4FC0E3</td>\n",
       "      <td>1</td>\n",
       "      <td>Horn Concerto No. 4 in E flat K495: II. Romanc...</td>\n",
       "      <td>Mozart - Eine kleine Nachtmusik</td>\n",
       "      <td>Barry Tuckwell/Academy of St Martin-in-the-Fie...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4e11f45d732f4861772b2906f81a7d384552ad12</td>\n",
       "      <td>SOFRQTD12A81C233C0</td>\n",
       "      <td>2</td>\n",
       "      <td>Sehr kosmisch</td>\n",
       "      <td>Musik von Harmonia</td>\n",
       "      <td>Harmonia</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  4e11f45d732f4861772b2906f81a7d384552ad12  SOCKSGZ12A58A7CA4B             1   \n",
       "1  4e11f45d732f4861772b2906f81a7d384552ad12  SOCVTLJ12A6310F0FD             1   \n",
       "2  4e11f45d732f4861772b2906f81a7d384552ad12  SODLLYS12A8C13A96B             3   \n",
       "3  4e11f45d732f4861772b2906f81a7d384552ad12  SOEGIYH12A6D4FC0E3             1   \n",
       "4  4e11f45d732f4861772b2906f81a7d384552ad12  SOFRQTD12A81C233C0             2   \n",
       "\n",
       "                                               title  \\\n",
       "0               Bleed It Out [Live At Milton Keynes]   \n",
       "1                                             Clocks   \n",
       "2                                          Breakeven   \n",
       "3  Horn Concerto No. 4 in E flat K495: II. Romanc...   \n",
       "4                                      Sehr kosmisch   \n",
       "\n",
       "                                     release  \\\n",
       "0  Road To Revolution: Live At Milton Keynes   \n",
       "1                A Rush Of Blood To The Head   \n",
       "2           Now That's What I Call Music! 72   \n",
       "3            Mozart - Eine kleine Nachtmusik   \n",
       "4                         Musik von Harmonia   \n",
       "\n",
       "                                         artist_name  year  \n",
       "0                                        Linkin Park  2008  \n",
       "1                                           Coldplay  2002  \n",
       "2                                         The Script  2008  \n",
       "3  Barry Tuckwell/Academy of St Martin-in-the-Fie...     0  \n",
       "4                                           Harmonia     0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('triplet_dataset_sub_song_merged.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    " #Item_CF_Recommender类，里面的操作需要vincent自行实现  begin\n",
    "from SVD_CF_Recommender import SVD_CF  \n",
    " #Item_CF_Recommender类，里面的操作需要vincent自行实现  end\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train, test = train_test_split(data, test_size = 0.40, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5656</th>\n",
       "      <td>8ad6a386e7b601c0072c337ec6f86d084d1ba556</td>\n",
       "      <td>SOXLOQG12AF72A2D55</td>\n",
       "      <td>1</td>\n",
       "      <td>Unite (2009 Digital Remaster)</td>\n",
       "      <td>Hello Nasty (Remastered Edition)</td>\n",
       "      <td>Beastie Boys</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29368</th>\n",
       "      <td>1272d32979a6ecd4f8b48cf25c9738c2298e0b4f</td>\n",
       "      <td>SONOYIB12A81C1F88C</td>\n",
       "      <td>2</td>\n",
       "      <td>I Kissed A Girl</td>\n",
       "      <td>One Of The Boys (iTunes Exclusive)</td>\n",
       "      <td>Katy Perry</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12984</th>\n",
       "      <td>a41d3edbc2798b6800fe15845a979150eb244b85</td>\n",
       "      <td>SOJYBJZ12AB01801D0</td>\n",
       "      <td>4</td>\n",
       "      <td>Shots</td>\n",
       "      <td>Party Rock</td>\n",
       "      <td>LMFAO / Lil Jon</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30988</th>\n",
       "      <td>480f3a551bbcdb53f8eb28e44614aa7ce0cc8d0e</td>\n",
       "      <td>SOTRDVJ12A8C13975E</td>\n",
       "      <td>2</td>\n",
       "      <td>Savin' Me</td>\n",
       "      <td>NOW (That's What I Call Music) 22</td>\n",
       "      <td>Nickelback</td>\n",
       "      <td>2005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24270</th>\n",
       "      <td>0252acdea2a493da2704c23eebaeaa155b18b7d0</td>\n",
       "      <td>SOLCKAS12A8C14242A</td>\n",
       "      <td>7</td>\n",
       "      <td>Eye Of The Tiger</td>\n",
       "      <td>Happy New Year!</td>\n",
       "      <td>Survivor</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           user                song  \\\n",
       "5656   8ad6a386e7b601c0072c337ec6f86d084d1ba556  SOXLOQG12AF72A2D55   \n",
       "29368  1272d32979a6ecd4f8b48cf25c9738c2298e0b4f  SONOYIB12A81C1F88C   \n",
       "12984  a41d3edbc2798b6800fe15845a979150eb244b85  SOJYBJZ12AB01801D0   \n",
       "30988  480f3a551bbcdb53f8eb28e44614aa7ce0cc8d0e  SOTRDVJ12A8C13975E   \n",
       "24270  0252acdea2a493da2704c23eebaeaa155b18b7d0  SOLCKAS12A8C14242A   \n",
       "\n",
       "       listen_count                          title  \\\n",
       "5656              1  Unite (2009 Digital Remaster)   \n",
       "29368             2                I Kissed A Girl   \n",
       "12984             4                          Shots   \n",
       "30988             2                      Savin' Me   \n",
       "24270             7               Eye Of The Tiger   \n",
       "\n",
       "                                  release      artist_name  year  \n",
       "5656     Hello Nasty (Remastered Edition)     Beastie Boys     0  \n",
       "29368  One Of The Boys (iTunes Exclusive)       Katy Perry  2008  \n",
       "12984                          Party Rock  LMFAO / Lil Jon  2008  \n",
       "30988   NOW (That's What I Call Music) 22       Nickelback  2005  \n",
       "24270                     Happy New Year!         Survivor     0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "svd_recommender=SVD_CF(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the rmse on test data is  39.93318749832018\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/vincent/5th week-base/SVD_CF_Recommender.py:107: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  test_X['prd_play_count']=output\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision:0.6073845293090282 recall:0.6073845293090282\n"
     ]
    }
   ],
   "source": [
    "#对测试集中每个用户产生歌曲点击预测\n",
    "recommendations = svd_recommender.test(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14954"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(recommendations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
